package com.wusuowei.shiro_jwt.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wusuowei.shiro_jwt.annotation.CrudLog;
import com.wusuowei.shiro_jwt.common.enums.BusinessType;
import com.wusuowei.shiro_jwt.model.po.Category;
import com.wusuowei.shiro_jwt.model.po.Product;
import com.wusuowei.shiro_jwt.service.CategoryService;
import com.wusuowei.shiro_jwt.service.ProductService;
import com.wusuowei.shiro_jwt.service.RoleService;
import com.wusuowei.shiro_jwt.utils.R;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *
 * @author 夏沫止水
 * @email HeJieLin@gulimall.com
 * @date 2024-05-01 14:03:02
 */
@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;
//    @Autowired
//    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    //@RequiresPermissions(":product:list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = productService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions(":product:info")
    public R info(@PathVariable("id") Integer id){
		Product product = productService.getById(id);

        return R.ok().put("product", product);
    }

    /**
     * 保存以及修改
     */

//    @RequiresRoles(value = {"admin","vip"},logical = Logical.OR)
//    @Transactional
//    @PostMapping
//    @CrudLog(title = "添加商品",businessType = BusinessType.INSERT)
//    public R save(@RequestBody Product product){
//		productService.saveOrUpdate(product);
//
//        return R.ok();
//    }

    /**
     * 保存
     * @param product
     * @return
     */
    @RequiresRoles(value = {"admin", "vip"}, logical = Logical.OR)
    @Transactional
    @PostMapping("/add")
    @CrudLog(title = "添加商品", businessType = BusinessType.INSERT)
    public R save(@RequestBody Product product) {
        // 只有当商品ID为null时才视为新商品添加
        if (product.getId() == null) {
            productService.save(product);
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     * @param product
     * @return
     */
    @RequiresRoles(value = {"admin", "vip"}, logical = Logical.OR)
    @Transactional
    @PostMapping("/update")
    @CrudLog(title = "修改商品", businessType = BusinessType.UPDATE)
    public R update(@RequestBody Product product) {
        // 只有当商品ID非null时才视为更新操作
        if (product.getId() != null) {
            productService.updateById(product);
            return R.ok();
        }
        return R.error();
    }

    /**
    单个删除

    */
    @RequiresRoles("admin")
    @DeleteMapping("/{id}")
    @CrudLog(title = "删除商品",businessType = BusinessType.DELETE)
    public R delete(@PathVariable Integer id) {
        productService.removeById(id);

        return R.ok();
    }
    /**
     * 批量删除
     */
    @RequestMapping("/del/batch")
    @RequiresRoles("admin")
    @Transactional
    @CrudLog(title = "删除商品",businessType = BusinessType.DELETE)
    //@RequiresPermissions(":product:delete")
    public R delete(@RequestBody List<Integer> ids){

        productService.removeBatchByIds(ids);

        return R.ok();
    }
    /**
     * @description 查找页面
     * @param pageNum 书籍页码
     * @param pageSize 页面大小
     * @param proname 商品名
     * @param cname 分类名

     * @return {@link Map }<{@link String }, {@link Object }>
     * @author LGY
     * @date 2023/03/24 21:11
     */
    @RequiresRoles(value = {"admin","vip","sysUser"},logical = Logical.OR)//满足一个角色就能访问
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(required = false) String proname,
                                        @RequestParam(required = false) String cname,
                                        @RequestParam(required = false) String proprice) {

        Page<Product> page = productService.findPage(new Page<>(pageNum, pageSize), proname, cname,proprice);
        List<Product> records = page.getRecords();
//        System.out.println("传进来的cname的值-----："+cname);
        System.out.println("list中的product-----："+records);
//        List<Product> collect = records.stream().map(item -> {
//            item.setRole(roleService.getRoles(String.valueOf(item.getId())));
//            return item;
//        }).collect(Collectors.toList());
//        page.setRecords(collect);
        //设置cname


        List<Product> collect2 = records.stream().map(item -> {
            Category category = categoryService.getCategory(String.valueOf(item.getCid()));
            item.setCategory(category);
            return item;
        }).collect(Collectors.toList());

        page.setRecords(collect2);
        List<Product> records2 = page.getRecords();
        System.out.println("list2中的product-----："+records2);
        return R.ok().setData(page);
    }

}
